﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using SubSonic.Extensions;
using System.Linq;
using System.Data;
using System.Web;

namespace Eacademy.Library.Helpers
{
    public static class AutomapperExtensions
    {
        public static object DataRowMapper(DataRow dataRow, Type type, string prefix)
        {
            try
            {
                var returnObject = Activator.CreateInstance(type);

                foreach (var property in type.GetProperties())
                {
                    foreach (DataColumn key in dataRow.Table.Columns)
                    {
                        string columnName = key.ColumnName;
                        if (!String.IsNullOrEmpty(dataRow[columnName].ToString()))
                        {
                            if (String.IsNullOrEmpty(prefix) || columnName.Length > prefix.Length)
                            {
                                String propertyNameToMatch = String.IsNullOrEmpty(prefix) ? columnName : columnName.Substring(property.Name.IndexOf(prefix) + prefix.Length + 1);
                                propertyNameToMatch = propertyNameToMatch.ToPascalCase();
                                if (property.Name == propertyNameToMatch)
                                {

                                    Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

                                    object safeValue = (dataRow[columnName] == null) ? null : Convert.ChangeType(dataRow[columnName], t);

                                    // Convert.ChangeType(collection[key], property.PropertyType)

                                    property.SetValue(returnObject, safeValue, null);
                                }
                            }
                            //else if (property.GetValue(returnObject, null) == null)
                            //{
                            //    property.SetValue(returnObject, RecurseType(collection, property.PropertyType, String.Concat(prefix, property.PropertyType.Name)), null);
                            //}
                        }
                    }
                }

                return returnObject;
            }
            catch (MissingMethodException)
            {

                return null;
            }
        }

        public static object RequestMapper(NameValueCollection collection, Type type, string prefix)
        {
            try
            {
                var returnObject = Activator.CreateInstance(type);

                foreach (var property in type.GetProperties())
                {
                    foreach (String key in collection.AllKeys)
                    {
                        string columnName = key;
                        if (!String.IsNullOrEmpty(collection[columnName].ToString()))
                        {
                            if (String.IsNullOrEmpty(prefix) || columnName.Length > prefix.Length)
                            {
                                String propertyNameToMatch = String.IsNullOrEmpty(prefix) ? columnName : columnName.Substring(property.Name.IndexOf(prefix) + prefix.Length + 1);
                                String propertyName = property.Name.ToLower();
                                if (propertyName == propertyNameToMatch)
                                {

                                    Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

                                    object safeValue = (collection[columnName] == null) ? null : Convert.ChangeType(collection[columnName], t);

                                    // Convert.ChangeType(collection[key], property.PropertyType)

                                    property.SetValue(returnObject, safeValue, null);
                                }
                            }
                        }
                    }
                }

                return returnObject;
            }
            catch (MissingMethodException)
            {

                return null;
            }
        }
    }
}